package com.sd.util;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.alex.books.BaseBook;
import com.alex.database.BookDao;
import com.alex.http.ADownloadListener;
import com.alex.http.AHandledResult;
import com.alex.http.AHttpEngine;
import com.alex.http.AHttpListener;
import com.alex.http.AResourceRequest;
import com.sd.activity.r.R;
import com.sd.app.AppInit;
import com.sd.view.ToastManager;

public class DownloadChannel implements  AHttpListener, OnClickListener, ADownloadListener{

	private ProgressBar download;
	private BaseBook mBaseBook;
	private Context mContext;
	private int cur,allsize;
	
	public DownloadChannel(BaseBook book,RelativeLayout relative,Context context){
		mContext = context;
		mBaseBook= book;
		download = (ProgressBar)relative.findViewById(R.id.download_progress);
		
		System.err.println("mBookSerial:"+book.mBookSerial);
		String url = book.mAttach2;
	    String name = book.mBookPath.substring(book.mBookPath.lastIndexOf("/")+1);
	    
	    AResourceRequest res = new AResourceRequest(0L,url,name,this, this);
		res.setResouceDir(AppInit.getInstance().APP_BOOKS);
		res.setBeginDown("0");
		AHttpEngine.getInstance().request(res);
	}

	@Override
	public void onError(long requestId, Bundle paramBundle) {
		// TODO Auto-generated method stub
		ToastManager.getInstance().showText(mContext, "下载失败！");
		mBaseBook.mAttach2 = "0";
		BookDao.getInstance().updateBook(mContext, mBaseBook);
		DownloaderEngine.getInstance().getAdapter().notifyDataSetChanged();
	}

	@Override
	public void onResult(long requestId, Bundle paramBundle,
			AHandledResult paramHandledResult) {
		// TODO Auto-generated method stub
		mBaseBook.mAttach2 = "2";
		mBaseBook.mIsFirstReader = 1;
		BookDao.getInstance().deleteChapter(mContext, mBaseBook.mBookId);
		Log.e("", ""+mBaseBook.mBookName + "   "+mBaseBook.mAttach2 );
		BookDao.getInstance().updateBook(mContext, mBaseBook);
		ToastManager.getInstance().showText(mContext, R.string.msg_success_download);
		download.setProgress(0);
		download.setMax(244);
		DownloaderEngine.getInstance().getAdapter().notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	
	public void setRelative(RelativeLayout relative){
		download = (ProgressBar)relative.findViewById(R.id.download_progress);
		Log.e("DownloadChannel", "cur:"+cur+"allsize:"+allsize);
		download.setProgress(cur);
		download.setMax(allsize);
		DownloaderEngine.getInstance().getAdapter().notifyDataSetChanged();
	}
	
	StringBuffer sbuf = new StringBuffer();
	
	@Override
	public void updateDownloadData(long requestId, Bundle paramBundle,
			long curSize, long allSize) {
		// TODO Auto-generated method stub
		sbuf.append(curSize);
		sbuf.append("\\");
		sbuf.append(allSize);
		sbuf.append("\n");
		
		download.setProgress(cur);
		download.setMax(allsize);
		
		cur = (int) curSize;
		allsize = (int) allSize;

		//Log.e("", "sbuf:"+sbuf);
	}
}
